﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Core;
using System.Data;
using System.Web;

namespace Portal.Admin.Screen.Render
{
    public class Utility
    {
        public static SiteParam CreateSiteParam(string SiteId, string XmlSiteConfigFile)
        {
            string str;
            DataSet set;
            DataView view;
            SiteParam param = new SiteParam();
            if (HttpContext.Current.Session != null)
            {
                try
                {
                    if (HttpContext.Current.Session["SiteParam" + SiteId] != null)
                    {
                        return (SiteParam)HttpContext.Current.Session["SiteParam" + SiteId];
                    }
                    str = XmlSiteConfigFile;
                    set = new DataSet();
                    set.ReadXml(str);
                    view = new DataView(set.Tables[0], "SiteId='" + SiteId + "'", "", DataViewRowState.CurrentRows);
                    if (view.Count == 0)
                    {
                        param = null;
                        return param;
                        //throw new Exception(SiteId + " not found in siteconfig.xml");
                    }
                    param.Parse(view[0]);
                    HttpContext.Current.Session["SiteParam" + SiteId] = param;
                }
                catch
                {
                    param = null;
                }
                return param;
            }

            try
            {
                str = XmlSiteConfigFile;
                set = new DataSet();
                set.ReadXml(str);
                view = new DataView(set.Tables[0], "SiteId='" + SiteId + "'", "", DataViewRowState.CurrentRows);
                if (view.Count == 0)
                {
                    param = null;
                    return param;
                    //throw new Exception(SiteId + " not found in siteconfig.xml");
                }
                param.Parse(view[0]);
                HttpContext.Current.Session["SiteParam" + SiteId] = param;
            }
            catch
            {
                param = null;
            }
            return param;
        }
    }
}
